package yxy.game.pm2.command.player;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlayerMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.playerCommand.PlayerCommandHandler;
import yxy.game.pm2.module.playerCommand.PlayerCommandModule;

/**
 * @author wrs
 * @since 2022-08-04
 * @descrition 玩家统御信息
 * */
@MessageType(proto = Command.PlayerCommandInfo, classtype = PlayerMessage.PlayerCommandInfoRequest.class)
public class CMD_PlayerCommandInfo extends Command<PlayerMessage.PlayerCommandInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, PlayerMessage.PlayerCommandInfoRequest request) throws Exception {
        String playerCode = messageSource.getTag();

        PlayerMessage.PlayerCommandInfoResponse.Builder response = PlayerMessage.PlayerCommandInfoResponse.newBuilder();

        PlayerCommandHandler handler = new PlayerCommandHandler();
        try {
            PlayerCommandModule playerCommandModule = PlayerCommandModule.get(playerCode);
            if (playerCommandModule == null){
                send(messageSource,response, Result.DataError);
            }

            Currency currency = handler.getCostCurrency(playerCommandModule);
            response.setRewards(new CurrencyHandle().packItem(currency));//花费的元宝

            Result result = handler.packCommandInfo(playerCommandModule,response);

            send(messageSource,response,result);

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void send(MessageSource messageSource, PlayerMessage.PlayerCommandInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
